/*
 * DailyQuotation.java
 * business
 *
 * Created by ChenTao on 2017/6/5.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.response.QueryDailyQuotationResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 每日行情-Model
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017-06-05 18:14
 */
public class DailyQuotationModel implements IDailyQuotationModel {

    private NetBiz mNetBiz = new NetBiz();

    private int mPager;

    private List<Object> mDisplayItems = new ArrayList<>();

    private List<QueryDailyQuotationResponse.BrandQuotation> mAllPagerData = new ArrayList<>();

    @Override

    public void loadDailyData(final boolean isRefreshing, String tag, final OnAccessNetListener<List<Object>> listener) {

        if (isRefreshing) {
            mPager = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(mPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setTag(tag);
        request.setUrl(Urls.DAILY_QUOTATION);

        mNetBiz.sendGetRequest(request, QueryDailyQuotationResponse.class, new NetListener<QueryDailyQuotationResponse>() {
            @Override
            public void onSuccess(QueryDailyQuotationResponse response) {

                mPager++;

                if (isRefreshing) {
                    mDisplayItems.clear();
                    mAllPagerData.clear();
                }

                // 获取上一页的最后一个数据
                QueryDailyQuotationResponse.BrandQuotation lastPagerLast = null;
                if (mAllPagerData != null && mAllPagerData.size() != 0) {
                    lastPagerLast = mAllPagerData.get(mAllPagerData.size() - 1);
                }

                // 新一页的第一个数据
                List<QueryDailyQuotationResponse.BrandQuotation> newPagerData = response.getData().getBrandQuotations();
                QueryDailyQuotationResponse.BrandQuotation newPagerFirst = null;
                if (newPagerData != null && newPagerData.size() != 0) {
                    newPagerFirst = newPagerData.get(0);
                }

                // 上一页最后一个数据与新一页的第一个数据进行对比，相同则合并，不同则拼接
                if (lastPagerLast != null && newPagerFirst != null && (lastPagerLast.equals(newPagerFirst))) {
                    // 相同，合并到上一页最后一个数据
                    lastPagerLast.getGoodsList().addAll(newPagerFirst.getGoodsList());
                    // 合并之后将新页面第一个数据移除
                    newPagerData.remove(newPagerFirst);

                    // 将新一页的第一个数据添加到展示的列表的末尾(也就是添加到上一页的末尾)
                    mDisplayItems.addAll(newPagerFirst.getGoodsList());
                }

                if (newPagerData != null) {

                    // 添加到上一页的末尾
                    mAllPagerData.addAll(newPagerData);

                    // 添加新一页展示的数据
                    for (QueryDailyQuotationResponse.BrandQuotation dailyData : newPagerData) {
                        mDisplayItems.add(dailyData.getBrandName());
                        mDisplayItems.addAll(dailyData.getGoodsList());
                    }
                }

                if (listener != null) {
                    listener.onSuccess(mDisplayItems, newPagerData == null || newPagerData.size() != 0,
                            response.hasMoreData(),response.getData().getShareEntity());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }
}
